﻿using System;

namespace Hvam.SharePoint.Search.Language.Parsers
{
    internal static class ResultExtensions
    {
        public static Result<U> IfSuccess<T, U>(this Result<T> result, Func<Success<T>, Result<U>> next)
        {
            var s = result as Success<T>;
            if (s != null)
            {
                return next(s);
            }

            var f = (Failure<T>) result;

            return new Failure<U>(f.Input);
        }

        public static Result<T> IfFailure<T>(this Result<T> result, Func<Failure<T>, Result<T>> next)
        {
            var s = result as Success<T>;
            if (s != null)
            {
                return s;
            }

            var f = (Failure<T>)result;
            return next(f);
        }
        
        public static bool IsSuccess<T>(this Result<T> result)
        {
            return result is Success<T>;
        }

        public static bool IsFailure<T>(this Result<T> result)
        {
            return result is Failure<T>;
        }
    }
}